﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace Assist
{
	public partial class DlgSynergyType : System.Windows.Forms.Form
	{
		public DlgSynergyType()
		{
			this.InitializeComponent();
		}

		public DlgSynergyType(int type)
		{
			this.InitializeComponent();
			this.dlgType = type;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.rbByFile.Checked)
			{
				this.synergyType = 0;
				this.FileName = this.tbFileName.Text;
				if (this.FileName == null)
				{
					MessageBox.Show("请先选择协同数据文件！");
					this.tbFileName.Focus();
					return;
				}
			}
			else
			{
				this.synergyType = 1;
			}
			base.DialogResult = DialogResult.OK;
		}

		public int SynergyType
		{
			get
			{
				return this.synergyType;
			}
			set
			{
				this.synergyType = value;
				if (this.synergyType == 0)
				{
					this.rbByFile.Checked = true;
					return;
				}
				this.rbByServer.Checked = true;
			}
		}

		public string FileName
		{
			get
			{
				return this.fileName;
			}
			set
			{
				this.fileName = value;
				this.tbFileName.Text = this.fileName;
			}
		}

		private void btnBrowse_Click(object sender, EventArgs e)
		{
			if (this.dlgType == 0)
			{
				SaveFileDialog saveFileDialog = new SaveFileDialog();
				saveFileDialog.Filter = "协同数据文件|*.xml";
				saveFileDialog.RestoreDirectory = true;
				saveFileDialog.FilterIndex = 1;
				if (saveFileDialog.ShowDialog() == DialogResult.OK)
				{
					this.tbFileName.Text = saveFileDialog.FileName;
					this.fileName = this.tbFileName.Text;
					return;
				}
			}
			else
			{
				OpenFileDialog openFileDialog = new OpenFileDialog();
				openFileDialog.Filter = "协同数据文件|*.xml";
				openFileDialog.RestoreDirectory = true;
				openFileDialog.FilterIndex = 1;
				if (openFileDialog.ShowDialog() == DialogResult.OK)
				{
					this.tbFileName.Text = openFileDialog.FileName;
					this.fileName = this.tbFileName.Text;
				}
			}
		}

		public void ReadDlgData(string cmdName)
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + cmdName + ".dat";
			if (!File.Exists(path))
			{
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			string value = streamReader.ReadLine();
			this.SynergyType = Convert.ToInt32(value);
			value = streamReader.ReadLine();
			this.FileName = value;
			streamReader.Close();
			fileStream.Close();
		}

		public void SaveDlgData(string cmdName)
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + cmdName + ".dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.SynergyType.ToString());
			streamWriter.WriteLine(this.FileName);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void rbByFile_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbByFile.Checked)
			{
				this.btnBrowse.Enabled = true;
				this.tbFileName.Enabled = true;
				return;
			}
			this.btnBrowse.Enabled = false;
			this.tbFileName.Enabled = false;
		}

		private void rbByServer_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbByFile.Checked)
			{
				this.btnBrowse.Enabled = true;
				this.tbFileName.Enabled = true;
				return;
			}
			this.btnBrowse.Enabled = false;
			this.tbFileName.Enabled = false;
		}

		private void tbFileName_TextChanged(object sender, EventArgs e)
		{
			this.fileName = this.tbFileName.Text;
		}

		private int dlgType;

		private int synergyType;

		private string fileName;
	}
}
